package
{
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.control.FControllers;
	import com.events.MyEvent;
	import com.model.ModelLocator;
	import com.model.vo.PlayState;
	import com.view.Timeline;
	import com.view.VideoPlayer;
	import com.view.lyric.KSCLyricParser;
	import com.view.lyric.LRCLyricParser;
	import com.view.lyric.LyricParser;
	
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.System;
	import flash.text.TextField;
	
	import mx.containers.Canvas;
	import mx.controls.Text;
	import mx.core.Application;
	import mx.core.UIComponent;
	
	public class PaiMakerClass extends Application
	{
		public var timeline:Timeline;
		public var videoPlayer:VideoPlayer;
		
		private var frontControllers:FControllers;
		
		public var source:String = "assets/青花瓷.mp3";
		public var lrc:String = "assets/青花瓷.ksc";
		
		private var topBackTxt:TextField;
		private var bottomBackTxt:TextField;
		private var topTxt:TextField;
		private var bottomTxt:TextField;
		
		private var sound:Sound;
		private var sc:SoundChannel;
		
		private var parser:LyricParser;
		private var lyricArr:Array;
		private var lrcRunList:Array = [];
		private var pausePosition:Number; // 暂停时的时间点		
		
		private var _model:ModelLocator;
		
		public function PaiMakerClass()
		{
			super();
			_model = ModelLocator.getInstance();
			frontControllers = new FControllers();
			addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
		}
		
		private function onAddToStage(event:Event):void
		{
			System.useCodePage=true;
			stage.scaleMode = StageScaleMode.NO_SCALE;
		}
		
		public function init(_source:String="", _lrc:String=""):void
		{
			source = _source;
			lrc = _lrc;
			
			topBackTxt = videoPlayer.topBackTxt;
			bottomBackTxt = videoPlayer.bottomBackTxt;
			topTxt = videoPlayer.topTxt;
			bottomTxt = videoPlayer.bottomTxt;
			if(lrc!="")	
			{
				var loader:URLLoader = new URLLoader();
				loader.addEventListener(Event.COMPLETE,loadFinish);
				loader.load(new URLRequest(lrc));
			}
			if(source)
			{
				sound = new Sound();
				sound.load(new URLRequest(source));
				sound.addEventListener(Event.ID3, onID3);
				sc = sound.play();
				sc.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
				
			}
		}
		
		private function onSoundComplete(event:Event):void
		{
			sc.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);
			stop();
		}
		
		private function onID3(event:Event):void
		{
			// 获取ID3 信息
			//sound.id3
		}
		
		private function loadFinish(evt:Event):void 
		{
			var list:String = evt.target.data;
			if(lrc.lastIndexOf(".lrc") != -1)
			{
				parser = new LRCLyricParser();
			}
			else
			{
				parser = new KSCLyricParser();
			}
			lyricArr = parser.parse( list );
			lrcRunList = parser.getRunList( lyricArr );
			_model.loaded = true;
			_model.playState = PlayState.PLAY;
			stage.addEventListener(Event.ENTER_FRAME,soundPlaying);
		}
		
		public var lastPosition:Number = 0; // 记录上一次的位置
		private function soundPlaying(evt:Event):void 
		{
			if(_model.playState != PlayState.PLAY)
				return;
			var now:Number = sc.position;
			
			var obj:Object = parser.getTxtByPosition(now);
			if(obj)
			{
				if(obj.bool)
				{	
					// 表示当前需要移动下层的遮罩
					topBackTxt.text = topTxt.text = obj.nextLyric;
					bottomBackTxt.text = bottomTxt.text = obj.currentLyric;
					parser.bottomSeek( now );
				}
				else
				{
					// 表示当前需要移动上面的遮罩
					topBackTxt.text = topTxt.text = obj.currentLyric;
					bottomBackTxt.text = bottomTxt.text = obj.nextLyric;
					parser.topSeek( now );
				}
			}
//trace("-------------width: ", videoPlayer.topMaskImg.width, videoPlayer.bottomMaskImg.width);
//			parser.seek( now ); // 单行seek
			
//			trace(topBackTxt.text, bottomBackTxt.text);
				
			timeline.percent = now/sound.length;
			timeline.timeLabel = ""+ changTime(now) + "/" + changTime(sound.length);
			lastPosition = now;
//			if( Math.abs(now-sound.length) < 100)
//				stop();
		}
		
		private function changTime(now:Number):String
		{
			var num:Number = now/1000;
			var mm:int; //分
			var ss:int; //秒
			mm = num/60;
			ss = num - mm*60;
			return "" + mm+":"+ ss;
		}
		
		public function play():void
		{
			_model.playState = PlayState.PLAY;
			sc = sound.play( pausePosition );
			trace("play", pausePosition);
		}
		
		public function pause():void
		{
			pausePosition = sc.position;
			_model.playState = PlayState.PAUSE;
			sc.stop();
			trace("pause ", pausePosition);
		}
		
		public function stop():void
		{
			_model.playState = PlayState.NONE;
			CairngormEventDispatcher.getInstance().dispatchEvent(new MyEvent(MyEvent.STOP));
			stage.removeEventListener(Event.ENTER_FRAME,soundPlaying);
			try
			{
				sound.close();
			}
			catch(e:Error)
			{
				trace(e);
			}
			finally
			{
				sound = null;
			}
			sc.stop();
			sc = null;
			pausePosition = 0;
		}

	}
}